/* 
 * Copyright 2011 VelocitiSoftware
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.velociti.ikarus.widget.client.ui;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Element;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.ui.VCssLayout;

/**
 * ######VIkarusDock##### -Simplified and modified version of
 * http://www.wizzud.com/jqDock/ -Check http://www.wizzud.com/jqDock/ site for
 * the usage. -Needs JQuery js and jqdock.js -Look at dock.css for styling.
 * 
 * @author Alper Turkyilmaz - VelocitiSoftware - 2011
 * @version 1.0.2
 * 
 */
public class VIkarusDock extends VCssLayout {
	private static String CLASSNAME = "v-ikarusdock";
	private static String MENU = "menu";
	private static String PAGE = "page";

	private ApplicationConnection client;
	private String id;

	public VIkarusDock() {
	}

	protected Element getContainerElement() {
		return getElement();
	}

	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		super.updateFromUIDL(uidl, client);
		this.client = client;
		id = uidl.getId();

		if (client.updateComponent(this, uidl, false)) {
			return;
		}
		getElement().setId("panel" + id);
		getWidget().getElement().setId("menu" + id);
		setStylePrimaryName(CLASSNAME);
		getWidget().setStylePrimaryName(CLASSNAME);
		setStyleDependentName(PAGE, true);
		getWidget().setStyleDependentName(MENU, true);
		if (uidl.hasAttribute("stylenames")) {
			String[] styles = uidl.getStringAttribute("stylenames").split(" ");
			for (int i = 0; i < styles.length; i++) {
				if (styles[i].length() > 0) {
					setStyleDependentName(PAGE + "-" + styles[i], true);
					getWidget().setStyleDependentName(MENU + "-" + styles[i], true);
				}
			}
		}

		IkarusJSDockOptions options = JavaScriptObject.createObject().cast();
		options.setActive(uidl.getIntAttribute("active"));
		options.setAlign(uidl.getStringAttribute("align"));
		options.setBias(uidl.getIntAttribute("bias"));
		options.setCoefficient(uidl.getDoubleAttribute("coefficient"));
		options.setDistance(uidl.getIntAttribute("distance"));
		options.setDuration(uidl.getIntAttribute("duration"));
		options.setFadeIn(uidl.getIntAttribute("fadeIn"));
		options.setFadeLayer(uidl.getStringAttribute("fadeLayer"));
		options.setFlow(uidl.getBooleanAttribute("flow"));
		options.setIdle(uidl.getIntAttribute("idle"));
		options.setInactivity(uidl.getIntAttribute("inactivity"));
		if (uidl.hasAttribute("uselabels")) {
			options.setLabelPosition(uidl.getStringAttribute("labels"));
		}
		options.setNoBuffer(uidl.getBooleanAttribute("noBuffer"));
		options.setSize(uidl.getIntAttribute("size"));
		options.setStep(uidl.getIntAttribute("step"));
		options.setLoader(uidl.getStringAttribute("loader"));
		dock(id, options);
	}

	@Override
	public void setWidth(String width) {
		super.setWidth(width);
		getWidget().getElement().getStyle().setWidth(getElement().getClientWidth(), Unit.PX);
	}

	public static native void dock(String id, IkarusJSDockOptions options)
	/*-{
	 		$wnd.$("#menu"+id).jqDock('destroy');
	 		$wnd.$("#menu"+id).jqDock(options);
	}-*/;

}
